<?php
/**
 * 用户微信服务层
 */
namespace app\service;
class UserWeixinService
{
	public static $web_appid = 'wx8f727f4e82c3392b'; //公众号appid
	public static $web_appsecret = 'b28ef5e767bbbde0c302806f14615d27'; //公众号appsecret
    
    /**
	 * 生成gzh access_token
	 */
    public static function get_wx_web_access_token()
    {
    	$wx_web_access_token = cache('user_wx_web_access_token');
		if(!empty($wx_web_access_token))
		{
			return $wx_web_access_token;
		}else
		{
			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::$web_appid."&secret=".self::$web_appsecret;
			$ret_json = file_get_contents($url);
			$ret = json_decode($ret_json,true);
			if(!empty($ret['access_token']))
			{
				cache('user_wx_web_access_token',$ret['access_token'],7200);
				return $ret['access_token'];
			}
		}
    }

    /**
     * 发送模板消息
     */
    public static function send_wx_web_tem_msg($params)
    {
    	$res_tem_msg = http_curl('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.self::get_wx_web_access_token(),json_encode($params));
		return $res_tem_msg;
	}
}